package cst.a114ly.startproject;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import cst.a114ly.startproject.bean.WeatherInfo;
import cst.a114ly.startproject.listener.WeatherListener;
import cst.a114ly.startproject.model.Weather51Model;
import cst.a114ly.startproject.model.WeatherModel;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,WeatherListener {
    private EditText cityid;
    private TextView city,temp,WD,WS;
    private Button button;
    private Button button2;
    void init(){
        cityid=(EditText)findViewById(R.id.cityid);
        city=(TextView)findViewById(R.id.city);
        temp=(TextView)findViewById(R.id.temp);
        button=(Button)findViewById(R.id.button);
        button2=(Button)findViewById(R.id.button2);
        WD=(TextView)findViewById(R.id.WD);
        WS=(TextView)findViewById(R.id.WS);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        button.setOnClickListener(this);
        button2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                WeatherModel weathermodel=new WeatherModel();
                weathermodel.getWeather(cityid.getText().toString(),this);break;
            case R.id.button2:
                Weather51Model weather51model=new Weather51Model();
                weather51model.getWeather(cityid.getText().toString(),this);break;
        }

    }

    @Override
    public void onResponse(WeatherInfo w) {
        if(w!=null){
            city.setText(w.getCity().toString());
            temp.setText(w.getTemp().toString());
            WD.setText(w.getWD().toString());
            WS.setText(w.getWS().toString());
        }else{
            city.setText("Error!");
            temp.setText("Error!");
        }
    }

    @Override
    public void onFail(String msg) {
        System.out.println(msg);
    }
}
